home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.4 / AppShell / Examples / Clipboard / dynamicimages.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  9.2 KB  |  334 lines

  1. /*************************************************************************
  2.   *                                                                      *
  3.   *                            Preliminary                               *
  4.   *                        Amiga AppShell (tm)                           *
  5.   *                                                                      *
  6.   *  Copyright (c) 1990,1991 Commodore-Amiga, Inc. All Rights Reserved.  *
  7.   *                                                                      *
  8.   *   This software and information is proprietary, preliminary, and     *
  9.   *   subject to change without notice.                                  *
  10.   *                                                                      *
  11.   *                            DISCLAIMER                                *
  12.   *                                                                      *
  13.   *   THIS SOFTWARE IS PROVIDED "AS IS".                                 *
  14.   *   NO REPRESENTATIONS OR WARRANTIES ARE MADE WITH RESPECT TO THE      *
  15.   *   ACCURACY, RELIABILITY, PERFORMANCE, CURRENTNESS, OR OPERATION      *
  16.   *   OF THIS SOFTWARE, AND ALL USE IS AT YOUR OWN RISK.                 *
  17.   *   NEITHER COMMODORE NOR THE AUTHORS ASSUME ANY RESPONSIBILITY OR     *
  18.   *   LIABILITY WHATSOEVER WITH RESPECT TO YOUR USE OF THIS SOFTWARE.    *
  19.   *                                                                      *
  20.   *                          Non-Disclosure                              *
  21.   *                                                                      *
  22.   *   This information is not to be disclosed to any other company,      *
  23.   *   individual or party.  Discussion is to be restricted to CBM        *
  24.   *   approved discussion areas, such as the closed conferences on bix;  *
  25.   *   amiga.cert, amiga.com, amiga.beta/appshell.                        *
  26.   *                                                                      *
  27.   ************************************************************************
  28.   * Copyright (C) 1990 Commodore-Amiga, Inc.
  29.   * written by David N. Junod
  30.   */
  31.  
  32. /*
  33. **    $Filename: dynamicimages.c $
  34. **    $Release: 1.4 $
  35. **    $Version: 3 $
  36. **
  37. **    images that can be drawn, filled, area filled and properly
  38. **    clipped.
  39. **
  40. **    (C) Copyright 1989 Commodore-Amiga, Inc.
  41. **        All Rights Reserved Worldwide
  42. **
  43. **    Revision History:
  44. **
  45. **    01-Dec-89  Created this file.            David N. Junod ~DNJ~
  46. **    09-Dec-89  Cleaned up for distribution.        ~DNJ~
  47. **    20-Dec-89  Added Application Flags        ~DNJ~
  48. **
  49. */
  50.  
  51. #include <exec/types.h>
  52. #include <exec/memory.h>
  53. #include <intuition/intuition.h>
  54. #include <graphics/gfx.h>
  55. #include <graphics/rastport.h>
  56. #include <clib/graphics_protos.h>
  57. #include <clib/intuition_protos.h>
  58. #include <clib/layers_protos.h>
  59.  
  60. #define AREA_SIZE 200
  61.  
  62. struct DynamicImage
  63. {
  64.     struct Image di_image;    /* Image structure to draw/display */
  65.     struct BitMap di_bmap;    /* Image's BitMap */
  66.     struct RastPort di_rport;    /* Image's RastPort */
  67.     ULONG di_size;        /* Size of ImageData */
  68.  
  69.     struct TmpRas di_tmpras;    /* Flood Fill TmpRas */
  70.     UBYTE *di_workspace;    /* Flood Fill workspace */
  71.  
  72.     struct AreaInfo di_area;    /* Area Fill AreaInfo */
  73.     WORD di_array[AREA_SIZE];    /* Area Fill workspace */
  74.  
  75.     struct Layer_Info *di_li;    /* Layer_Info for layer manipulations */
  76.     struct Layer *di_layer;    /* Layer structure */
  77.     ULONG Flags;        /* Application Flags -- see defines */
  78. };
  79.  
  80. /* Application Flags.
  81.  * Used to indicate what parts of the DynamicImage need
  82.  * to be initialized.
  83.  */
  84. #define DI_FILL        (1L<<0)
  85. #define    DI_LAYER    (1L<<1)
  86.  
  87. /* DynamicImages.c: Function Prototypes */
  88. VOID InitDynamicImage (struct DynamicImage *, USHORT, USHORT, USHORT);
  89. BOOL AllocDynamicImage (struct DynamicImage *);
  90. VOID FreeDynamicImage (struct DynamicImage *);
  91.  
  92. /* Private allocation functions */
  93. static BOOL DIAllocFill (struct DynamicImages *, USHORT, USHORT);
  94. static BOOL DIAllocLayer (struct DynamicImage *, USHORT, USHORT);
  95.  
  96. /****** DynamicImages/InitDynamicImage *******************************
  97. *
  98. *   NAME
  99. *       InitDynamicImage - Initialize DynamicImage structure.
  100. *
  101. *   SYNOPSIS
  102. *       InitDynamicImage (di, depth, width, height)
  103. *
  104. *       struct DynamicImage *di;
  105. *       USHORT depth, width, height;
  106. *
  107. *   FUNCTION
  108. *       Initializes various fields of the DynamicImage structure
  109. *       to correctly reflect the size of the dynamically allocated
  110. *       image.
  111. *
  112. *   INPUTS
  113. *       di     - Pointer to the DynamicImage structure.
  114. *       depth  - Depth of the desired image.
  115. *       width  - Width of the desired image.
  116. *       height - Height of the desired image.
  117. *
  118. *   BUGS
  119. *
  120. *   SEE ALSO
  121. *
  122. *       AllocDynamicImage(), FreeDynamicImage()
  123. *
  124. **********************************************************************
  125. *
  126. * Created:  01-Dec-89, David N. Junod
  127. *
  128. */
  129.  
  130. VOID InitDynamicImage (struct DynamicImage * di,
  131.                 USHORT d, USHORT w, USHORT h)
  132. {
  133.     register SHORT i;
  134.  
  135.     /* Initialize Image Structure */
  136.     di->di_image.LeftEdge = 0;
  137.     di->di_image.TopEdge = 0;
  138.     di->di_image.Width = w;
  139.     di->di_image.Height = h;
  140.     di->di_image.Depth = d;
  141.     di->di_image.ImageData = NULL;
  142.     di->di_image.PlanePick = (1 << d) - 1;
  143.     di->di_image.PlaneOnOff = 0;
  144.     di->di_image.NextImage = NULL;
  145.     di->di_workspace = NULL;
  146.  
  147.     /*
  148.      * Initialize BitMap.
  149.      */
  150.     InitBitMap (&di->di_bmap, (BYTE) d, (SHORT) w, (SHORT) h);
  151.  
  152.     /*
  153.      * Initialize RastPort to draw into.
  154.      */
  155.     InitRastPort (&di->di_rport);
  156.     di->di_rport.BitMap = &di->di_bmap;
  157.  
  158.     /*
  159.      * Initialize Area Fill.
  160.      */
  161.     for (i = 0; i < AREA_SIZE; i++)
  162.     di->di_array[i] = 0;
  163.     InitArea (&di->di_area, &di->di_array[0], AREA_SIZE / 5);
  164.     di->di_rport.AreaInfo = &di->di_area;
  165.  
  166.     /*
  167.      * Clear Layer Information
  168.      */
  169.     di->di_li = NULL;
  170.     di->di_layer = NULL;
  171. }
  172.  
  173. /****** DynamicImages/AllocDynamicImage ******************************
  174. *
  175. *   NAME
  176. *       AllocDynamicImage - Allocates memory required for DynamicImage
  177. *
  178. *   SYNOPSIS
  179. *       AllocDynamicImage (di)
  180. *
  181. *       struct DynamicImage *di;
  182. *
  183. *   FUNCTION
  184. *       Allocates the image data and other work areas required for
  185. *       dynamic images.
  186. *
  187. *   INPUTS
  188. *       di     - Pointer to the DynamicImage structure.
  189. *
  190. *   BUGS
  191. *
  192. *   SEE ALSO
  193. *
  194. *       InitDynamicImage(), FreeDynamicImage()
  195. *
  196. **********************************************************************
  197. *
  198. * Created:  01-Dec-89, David N. Junod
  199. *
  200. */
  201.  
  202. BOOL AllocDynamicImage (struct DynamicImage * di)
  203. {
  204.     struct BitMap *bm = &di->di_bmap;
  205.     USHORT depth = di->di_image.Depth;
  206.     USHORT width = di->di_image.Width;
  207.     USHORT height = di->di_image.Height;
  208.     SHORT i, planes;
  209.     LONG image_data;
  210.  
  211.     planes = RASSIZE (width, height);
  212.     di->di_size = (depth * planes);
  213.     if (di->di_image.ImageData =
  214.     (USHORT *) AllocMem (di->di_size, MEMF_CHIP | MEMF_CLEAR))
  215.     {
  216.     image_data = (LONG) di->di_image.ImageData;
  217.     for (i = 0L; i < depth; ++i)
  218.         bm->Planes[i] = (PLANEPTR) (image_data + i * planes);
  219.  
  220.     if (di->Flags & DI_FILL)
  221.         if (!(DIAllocFill (di, width, height)))
  222.         {
  223.         FreeDynamicImage (di);
  224.         return (FALSE);
  225.         }
  226.  
  227.     if (di->Flags & DI_LAYER)
  228.         if (!(DIAllocLayer (di, width, height)))
  229.         {
  230.         FreeDynamicImage (di);
  231.         return (FALSE);
  232.         }
  233.  
  234.     return (TRUE);
  235.     }
  236.     return (FALSE);
  237. }
  238.  
  239. /****** DynamicImages/FreeDynamicImage *******************************
  240. *
  241. *   NAME
  242. *       FreeDynamicImage - Deallocates memory required for DynamicImage
  243. *
  244. *   SYNOPSIS
  245. *       FreeDynamicImage (di)
  246. *
  247. *       struct DynamicImage *di;
  248. *
  249. *   FUNCTION
  250. *       Deallocates the image data and other work areas required for
  251. *       dynamic images.  The DynamicImage image structure that was
  252. *       passed, can now be passed backed thru another
  253. *       AllocDynamicImage() or it's resources can be freed up.
  254. *
  255. *   INPUTS
  256. *       di     - Pointer to the DynamicImage structure.
  257. *
  258. *   BUGS
  259. *
  260. *   SEE ALSO
  261. *
  262. *       InitDynamicImage(), AllocDynamicImage()
  263. *
  264. **********************************************************************
  265. *
  266. * Created:  01-Dec-89, David N. Junod
  267. *
  268. */
  269.  
  270. VOID FreeDynamicImage (struct DynamicImage * di)
  271. {
  272.     USHORT width = di->di_image.Width;
  273.     USHORT height = di->di_image.Height;
  274.  
  275.     if (di)
  276.     {
  277.     if (di->di_li)
  278.     {
  279.         if (di->di_layer)
  280.         DeleteLayer (NULL, di->di_layer);
  281.         DisposeLayerInfo (di->di_li);
  282.     }
  283.     if (di->di_workspace)
  284.     {
  285.         di->di_rport.TmpRas = NULL;
  286.         FreeRaster ((PLANEPTR) di->di_workspace, width, height);
  287.     }
  288.     if (di->di_image.ImageData)
  289.         FreeMem (di->di_image.ImageData, di->di_size);
  290.     di->di_layer = NULL;
  291.     di->di_li = NULL;
  292.     di->di_image.ImageData = NULL;
  293.     di->di_workspace = NULL;
  294.     }
  295. }
  296.  
  297. /* PRIVATE Initialization Routines */
  298.  
  299. static BOOL
  300. DIAllocFill (struct DynamicImage * di, USHORT width, USHORT height)
  301. {
  302.  
  303.     if (di->di_workspace = (UBYTE *) AllocRaster (width, height))
  304.     {
  305.     /* Initialize Flood Fill Area */
  306.     di->di_rport.TmpRas = (struct TmpRas *)
  307.     InitTmpRas (&di->di_tmpras, di->di_workspace,
  308.             RASSIZE (width, height));
  309.     di->di_rport.TmpRas = &di->di_tmpras;
  310.     return (TRUE);
  311.     }
  312.     return (FALSE);
  313. }
  314.  
  315. static BOOL
  316. DIAllocLayer (struct DynamicImage * di, USHORT width, USHORT height)
  317. {
  318.  
  319.     if (di->di_li = NewLayerInfo ())
  320.     {
  321.     if (di->di_layer =
  322.         CreateUpfrontLayer (di->di_li, &di->di_bmap,
  323.                 0, 0, (width - 1), (height - 1),
  324.                 LAYERSIMPLE, NULL))
  325.     {
  326.         di->di_rport.Layer = di->di_layer;
  327.         return (TRUE);
  328.     }
  329.     DisposeLayerInfo (di->di_li);
  330.     }
  331.     return (FALSE);
  332. }
  333.  
  334.